<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Api\WxUser;
use App\Services\Curl;
use App\Services\Token;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ClientLoginController extends Controller
{
    //客户端登录
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 客户端登录
     * 手机号登录
     */
    public function getCode(Request $request)
    {
        $phone=$request->input('phone');
        if (empty($phone)){
            return response()->json(['code'=>1,'msg'=>'手机号不能为空','data'=>null]);
        }
        $res=Cache::get("$phone");
        if ($res){
            return response()->json(['code'=>1,'msg'=>'验证码未过期','data'=>null]);
        }
        $num=Cache::get($phone.date('Y-m-d'));
        if (5<$num){
            return response()->json(['code'=>1,'msg'=>'超过发送限制','data'=>null]);
        }
        $code=rand(100000,999999);
        Cache::put("$phone",$code,300);
        $num=$num+1;
        Cache::put($phone.date('Y-m-d'),$num,24*3600);
        return response()->json(['code'=>0,'msg'=>'发送成功','data'=>$code]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 用户手机号登录
     */
    public function phoneLogin(Request $request)
    {
        $phone=$request->input('phone');
        $code=$request->input('code');
        $coded=Cache::get("$phone");
        if ($code != $coded){
            return response()->json(['code'=>1,'msg'=>'验证码错误','data'=>null]);
        }
        $res=WxUser::wxLogin($phone);
        if ($res){
            $token=Token::createToken($res['id']);
            Cache::put("$token",$res['id']);
            return  response()->json(['code'=>0,'msg'=>'登录成功','data'=>['token'=>$token]]);
        }else{
            return  response()->json(['code'=>1,'msg'=>'登录失败','data'=>null]);
        }
    }

    public function wxLogin(Request $request)
    {
        $params = $request->all();
        $js_code = $params['code'];
        $login_url = sprintf(config('wx.login_url'), $js_code);
        //封装curl
        $data = Curl::geturl($login_url);
        $openid = $data['openid'];
        $session_key = $data['session_key'];
        $userInfo = WxUser::getUserInfo($openid);
        if ($userInfo) {
            $user_id = $userInfo->id;
            $updateData = [
                'session_key' => $session_key,
                'nickname' => $params['nickname'],
                'avatar' => $params['avatar']
            ];
            $updateRes =WxUser::updateUserInfo($user_id,$updateData);
            if (!$updateRes){
                return ['code'=>10004,'msg'=>'登录失败','data'=>[]];
            }
            $token = Token::createToken($user_id);
            return ['code'=>10001,'msg'=>'登录成功','data'=>['token'=>$token]];
        }
        $createData = [
            'nickname' => $params['nickname'],
            'avatar' => $params['avatar'],
            'openid' => $openid,
            'session_key' => $session_key,
        ];
        $createRes = WxUser::createUser($createData);
        if (!$createRes){
            return ['code'=>10004,'msg'=>'登录失败','data'=>[]];
        }
        $token = Token::createToken($createRes->id);
        return ['code'=>10001,'msg'=>'登录成功','data'=>['token'=>$token]];
    }
}
